<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#	
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
# install.php:
#
#		Helps in the installation of compress url.
##############################################################################################
?>
<html>
<head>
	<style>
		.success {
			color: #00cc00;
			font-weight:bold;
		}
		.error {
				color: #cc0000;
				font-weight:bold;
		}
		body {
			background:#f7f7f7;
		}
		#content {
			background:#ffffff;
			border:solid 2px #cccccc;
			width:500px;
			padding:20px;
			margin-top:50px;
		}
		#footer {
			float: right;
			font-size:14px;
			font-style:italic;
		}
		.bookmark {
			background:#FFF9D7; 
			border: solid 1px #E2C822; 
			font-weight: bold; 
			padding:2px; 
			padding-left:10px; 
			margin:5px;
			display:block;
		}
	</style>
</head>

<body>
	<center>
		<div id="content" style="text-align:left;">
		<h1 style="text-align:center; padding:10px; margin:0px;">Compress URL Installation</h1>
		<?php
			require("functions.inc.php");
			
			error_reporting(E_ERROR);

			echo "Checking if apache module <b>mod_rewrite</b> is loaded...";

			// Get all loaded apache modules
			if(function_exists("apache_get_modules")) {
				$modules = apache_get_modules();
				if ( $DEBUG ) print_r($modules);
				$mod_rewrite = false;
				
				// search for mod_rewrite
				foreach ( $modules as $module) {
					if ( $module == "mod_rewrite" ) {
						// found it
						$mod_rewrite = true;
						break;
					}	
				}
				if($mod_rewrite) {
					echo "<span class='success'>Succeeded.</span><br/><br/>";
				}
				else {
					echo "<span class='error'>Failed.</span>
					You can enable it by uncommenting the lines in httpd.conf and restarting the server. <br/>
					If you are not able to, then you need to do these steps : 
					<ol>
						<li>Rename 'url_compressor.php' to 'index.php'</li>
						<li>Move the directory containing 'url_compressor.php' and the directory 'url_compressor_libs'
								to a conveniently named directory, say 't'</li>
						<li>Set $"."MOD_REWRITE = false in config.inc.php .</li>
						<li>Run this setup from the new directory again.</li>
					</ol>";
				}
			}
			else {
				echo "<span class='error'>Cant tell.</span><br/>
							Cant find function apache_get_modules().. :( <br/>
							<a href='".get_path()."phpinfo.php'>Go here</a> and (manually) search for 'mod_rewite'.
							<br/>If it is present in the loaded modules, everything is fine. If it is not, try to see if pretty urls work. If they dont reset <b>$"."MOD_REWRITE</b> to false in config.inc.php<br/><br/>";
			}			
			echo "Reading $"."<b>MOD_REWRITE</b> from config.inc.php ...";
			if($MOD_REWRITE) {
				echo "<span class='success'>true</span>.
							You will have pretty URLs like <span style='color:#0000cc'>".get_outer_path()."tinyurl</span><br/>";
			}
			else {
				echo "<span class='error'>false</span>.
							You will have uglier URLs like <span style='color:#0000cc'>".get_outer_path()."tinyurl</span><br/>";
			}
			
			echo "<br/>Testing MySQL connection ... ";
			if ( mysql_pconnect ( $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD ) ) 
				echo "<span class='success'>Succeeded.</span><br/>";
			else 
				echo "<span class='error'>Failed.</span>
							Please check config.inc.php and set proper MySQL host, user and password.</span><br/>";
			
			echo "<br/>Selecting database <b>".$MYSQL_DATABASE."</b>...";
			
			if( mysql_select_db ( $MYSQL_DATABASE ) ) 
				echo "<span class='success'>Succeeded.</span><br/>";
			else 
				echo "<span class='error'>Failed.</span>
							Please check config.inc.php and set proper MySQL database name.<br/>";
			
			echo "<br/>Creating table <b>".$MYSQL_TABLE."</b>...";
			
			if(mysql_query("CREATE TABLE `".$MYSQL_TABLE."` (
										`id` int(11) NOT NULL auto_increment,
										`to` varchar(100) NOT NULL default '',
										`from` text NOT NULL,
										PRIMARY KEY  (`id`),
										UNIQUE KEY `from` (`to`)
										) TYPE=MyISAM AUTO_INCREMENT=2;"))

			echo "<span class='success'>Succeeded.</span><br/>" ;
			else {
				echo "<span class='error'>";
				print(mysql_error());
				echo "</span><br/>";
			}
			
			// insert add path into the table so that the user does not add it as a shortened url
			mysql_query("REPLACE INTO `".$MYSQL_TABLE."` VALUES (1, '".$ADD_PATH."', 'DO NOT DELETE THIS');")
				or die(mysql_error());
			
			echo "<br/>Running test query ...";
			
			$r = mysql_query("select count(id) from ".$MYSQL_TABLE);
			$a = mysql_fetch_array($r);
			if ( $DEBUG ) echo $a[0];
			if( $a[0] >= 1 )
				echo "<span class='success'>Succeeded.</span><br/>";
			else 
				echo "<span class='error'>Failed. </span>Please see if the table ".$MYSQL_TABLE." exists.<br/>";
			
			echo "<br/>If everything goes fine, you can compress URLs by 
						<a href='".get_outer_path().$ADD_PATH."'>going here</a>.<br/>";
			
			echo '<br/><div class="bookmark">Bookmark this link : <a href="javascript:document.location=(\'';
			
			if($MOD_REWRITE)
				echo get_outer_path().$ADD_PATH."?"; 
			else
				echo get_outer_path().$ADD_PATH."&from="; 
			echo '\'+document.location);">Compress URL</a> (by dragging this link onto your bookmarks toolbar) 
			and you can compress the url of the current window by a single click on it, any time, any website.</div>';
		?>
		<div id="footer">
			Developed by <a href="mailto:vermaabhishekp@gmail.com">Abhishek Verma</a>
		</div>
	</div>
</center>
</body>
</html>